package com.linkage.seapeak.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.linkage.jz.bankruptcy.R;

import java.lang.reflect.Field;

/**
 * Created by htsun on 16-6-23.
 */
public class ToastUtil {
    private Toast toast;
    private TextView toastText;
    private ImageView toastIcon;
    private static ToastUtil toastUtil;

    private Activity context = null;

    /**
     * icon
     */
    public enum TOAST_TYPE {
        NORMAL, ERROR, WARNING, SUCCESS, DOWN,NOSHOW
    }

    private ToastUtil(Activity con) {
        this.context = con;
        if (context == null) return;
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getToast();
            }
        });
    }

    public static ToastUtil getInstance(Activity context) {
        if (toastUtil == null) toastUtil = new ToastUtil(context);
        return toastUtil;
    }

    private void getToast() {
        View view = View.inflate(context, R.layout.view_custom_toast, null);
        toastText = (TextView) view.findViewById(R.id.toast_text_top);
        toastIcon = (ImageView) view.findViewById(R.id.toast_icon);

        toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        try {
            Object mTN;
            mTN = getField(toast, "mTN");
            if (mTN != null) {
                Object mParams = getField(mTN, "mParams");
                if (mParams != null
                        && mParams instanceof WindowManager.LayoutParams) {
                    WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;
                    params.width = -1;//-1表示全屏, 你也可以设置任意宽度.
                    params.height = -2;// (int) dpToPx(context, T_HEIGHT);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射字段
     *
     * @param object    要反射的对象
     * @param fieldName 要反射的字段名称
     */
    private static Object getField(Object object, String fieldName)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = object.getClass().getDeclaredField(fieldName);
        if (field != null) {
            field.setAccessible(true);
            return field.get(object);
        }
        return null;
    }

    /**
     * @param resId
     * @param type
     */
    public void showToast(final int resId, TOAST_TYPE type) {
        showToast(resId, 2300, type);
    }

    /**
     * @param msg
     * @param type
     */
    public void showToast(final CharSequence msg, TOAST_TYPE type) {
        showToast(msg, 2300, type);
    }


    public void showShortToast(CharSequence msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }

    public void showShortToast(int resId) {
        showToast(resId, Toast.LENGTH_SHORT);
    }

    public void showLongToast(CharSequence msg) {
        showToast(msg, Toast.LENGTH_LONG);
    }

    public void showLongToast(CharSequence msg, TOAST_TYPE type) {
        showToast(msg, Toast.LENGTH_LONG, type);
    }

    public void showLongToast(int resId) {
        showToast(resId, Toast.LENGTH_LONG);
    }

    private void showToast(final int resId, final int duration) {
        showToast(resId, duration, TOAST_TYPE.NORMAL);
    }

    private void showToast(final CharSequence msg, final int duration) {
        showToast(msg, duration, TOAST_TYPE.NORMAL);
    }

    private void showToast(final CharSequence msg, final int duration, final TOAST_TYPE toastType) {
        if (TextUtils.isEmpty(msg))
            return;
        try {
            if (context == null) return;
            context.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    int iconResId;
                    switch (toastType) {
                        case ERROR:
                            iconResId = R.drawable.ic_toast_error;
                            break;
                        case WARNING:
                            iconResId = R.drawable.ic_toast_base;
                            break;
                        case SUCCESS:
                            iconResId = R.drawable.ic_toast_success;
                            break;
                        case DOWN:
                            iconResId = R.drawable.ic_toast_down;
                            break;
                        case NOSHOW:
                            iconResId = -1;
                            break;
                        case NORMAL:
                        default:
                            iconResId = R.drawable.ic_toast_base;
                            break;
                    }
                    toastText.setText(msg);
                    if(iconResId !=-1) {
                        toastIcon.setImageResource(iconResId);
                    }else{
                        toastIcon.setVisibility(View.GONE);
                    }
                    toast.setDuration(duration);
//                    toast = Toast.makeText(context, msg, duration);
                    toast.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void showToast(final int resId, final int duration, final TOAST_TYPE toastType) {
        try {
            if (context == null) return;
            context.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    int iconResId;
                    switch (toastType) {
                        case ERROR:
                            iconResId = R.drawable.ic_toast_error;
                            break;
                        case WARNING:
                            iconResId = R.drawable.ic_toast_base;
                            break;
                        case SUCCESS:
                            iconResId = R.drawable.ic_toast_success;
                            break;
                        case DOWN:
                            iconResId = R.drawable.ic_toast_down;
                            break;
                        case NORMAL:
                        default:
                            iconResId = R.drawable.ic_toast_base;
                            break;
                    }

                    toastText.setText(resId);
                    toastIcon.setImageResource(iconResId);
                    toast.setDuration(duration);
//                    toast = Toast.makeText(context, resId, duration);
                    toast.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
